﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using Common;
using Basic.Model.Entity;
using Basic.Model.EntityManager;

namespace Basic.Business
{
    public class RoleGroupAdmin
    {
        /// <summary>
        /// 添加一个权限组
        /// </summary>
        /// <param name="roleGroupName"></param>
        public static void AddRoleGroup(string roleGroupName)
        {
            ISession session = null;
            ITransaction trans = null;
            RoleGroup roleGroup = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                if (RoleGroupManager.GetByCondition(session, -1, roleGroupName, false) != null)
                    throw new Exception("当前系统已存在这个权限组，请换一个权限组名。");
                roleGroup = new RoleGroup();
                roleGroup.roleGroupName = roleGroupName;
                RoleGroupManager.Add(session, roleGroup);

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        /// <summary>
        /// 查找一个权限组
        /// </summary>
        /// <param name="roleGroupName"></param>
        public static Basic.VO.RoleGroup GetRoleGroup(string roleGroupName)
        {
            ISession session = null;
            ITransaction trans = null;
            RoleGroup data = null;
            Basic.VO.RoleGroup roleGroup=null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                data = RoleGroupManager.GetByCondition(session, -1, roleGroupName, false);
                if (data != null)
                {
                    roleGroup.roleGroupId = data.roleGroupId;
                    roleGroup.roleGroupName = data.roleGroupName;
                }

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            return roleGroup;
        }
        public static Basic.VO.RoleGroup GetRoleGroup(int roleGroupId)
        {
            ISession session = null;
            ITransaction trans = null;
            RoleGroup data = null;
            Basic.VO.RoleGroup roleGroup = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                data = RoleGroupManager.GetByCondition(session, roleGroupId, null, false);
                if (data != null)
                {
                    roleGroup.roleGroupId = data.roleGroupId;
                    roleGroup.roleGroupName = data.roleGroupName;
                }

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            return roleGroup;
        }
        /// <summary>
        /// 返回查询到VO.RoleGroup对象的result结果集
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static Result<Basic.VO.RoleGroup> GetAllByCondition(int rowIndex, int rowCount)
        {
            Result<Basic.VO.RoleGroup> result = new Result<Basic.VO.RoleGroup>();
            IList<RoleGroup> dataList = new List<RoleGroup>();
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                result.total = RoleGroupManager.GetAllByConditionCount(session);
                dataList = RoleGroupManager.GetAllByCondition(session, true, rowIndex, rowCount);
                result.data = new List<Basic.VO.RoleGroup>();
                for (int i = 0; i < dataList.Count; i++)
                {
                    Basic.VO.RoleGroup roleGroup = new Basic.VO.RoleGroup();
                    roleGroup.roleGroupId = dataList[i].roleGroupId;
                    roleGroup.roleGroupName = dataList[i].roleGroupName;
                    roleGroup.roles = new List<Basic.VO.Role>();
                    IEnumerator<Role> roles = dataList[i].roles.GetEnumerator();
                    while (roles.MoveNext())
                    {
                        Basic.VO.Role role = new Basic.VO.Role();
                        role.roleId = roles.Current.roleId;
                        role.roleName = roles.Current.roleName;
                        roleGroup.roles.Add(role);
                    }
                    result.data.Add(roleGroup);
                }
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            return result;
        }
    }
}
